﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class CommandUIDefinition: IXmlBuildable,IParentable, IChildDeletable
    {
        public List<ICommandUIDefinition> CommandUIDefinitions { get; set; }

        public CommandUIDefinition()
        {
            this.CommandUIDefinitions = new List<ICommandUIDefinition>();
        }

        public CommandUIDefinition(XmlNode node):this()
        {
            try
            {
                foreach (XmlNode commandUIXmlNode in node.ChildNodes)
                {
                    ICommandUIDefinition uiDefinition = UIDefinitionHelper.GetCommandUIDefFromXml(commandUIXmlNode);
                    this.CommandUIDefinitions.Add(uiDefinition);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void AddCommandUIDefinitionNode(XmlNode customActionNode)
        {
            try
            {
                XmlNode commandUIDefinitionNode = XmlHelper.AddNode(customActionNode, "CommandUIDefinition");

                // Add CommandUIDefinitions
                foreach (ICommandUIDefinition commandDefinition in CommandUIDefinitions)
                {
                    commandDefinition.AddNode(commandUIDefinitionNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("CommandUIDefinition");
                node.Tag = this;
                node.ContextMenuStrip = this.BuildContextMenu(node);

                foreach (ICommandUIDefinition cuid in this.CommandUIDefinitions)
                {
                    cuid.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode currentNode)
        {
            string[] controls = new string[]{"Button","CheckBox","ComboBox","ColorPicker","ContextualGroup",
                "Controls","DropDown","FlyoutAnchor","Gallery","GalleryButton","Group",
                "Groups","InsertTable","Label","MRUSplitButton","MaxSize","Menu","MenuSection","Scale",
                "Scaling","Spinner","Splitbutton","Tab","TextBox","ToggleButton"};

            return ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(currentNode, controls, false);
        }

        #region IXmlBuildable Members

        public void AddNode(XmlNode parentNode)
        {
            try
            {
                this.AddCommandUIDefinitionNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                this.CommandUIDefinitions.Add(child);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is ICommandUIDefinition)
                {
                    this.CommandUIDefinitions.Remove((ICommandUIDefinition)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
